package com.hengxinguotong.hxgtwygsp.ui.main.Text;

import com.hengxinguotong.hxgtwygsp.model.PageEvent;
import com.hengxinguotong.hxgtwygsp.ui.main.picture.PictureFragment;
import com.hengxinguotong.hxgtwygsp.utils.TimeDown;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.FragmentEvent;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2019/3/23 13:50
 */

public class TextPresenter implements TextContract.IPresenter
{
    private TextContract.IView mView;

    private TimeDown pageTimeDown;

    @Inject
    public TextPresenter()
    {

    }

    @Override
    public void bindView(TextContract.IView iView)
    {
        this.mView = iView;
        LifecycleTransformer<Long> transformer = ((TextFragment) mView).bindUntilEvent(FragmentEvent.DESTROY_VIEW);
        pageTimeDown = new TimeDown(transformer, () -> EventBus.getDefault().post(new PageEvent()), 20);
    }

    @Override
    public void dropView()
    {
        this.mView = null;
    }

    @Override
    public void start()
    {
        if (pageTimeDown != null)
            pageTimeDown.startTimer();
    }

    @Override
    public void stop()
    {
        if (pageTimeDown != null)
            pageTimeDown.stop();
    }

    @Override
    public void recoverRemainTime()
    {
        pageTimeDown.recoverRemainTime();
    }
}
